package com.xusheng.cli.service.impl;

import cn.hutool.core.util.StrUtil;
import com.xusheng.cli.common.base.MyPage;
import com.xusheng.cli.common.base.MyServiceImpl;
import com.xusheng.cli.entity.frontUserModule.FrontUser;
import com.xusheng.cli.entity.frontUserModule.VipConfig;
import com.xusheng.cli.mapper.VipRecordMapper;
import com.xusheng.cli.entity.vipRecordModule.VipRecord;
import com.xusheng.cli.req.VipRecordQueryReq;
import com.xusheng.cli.service.VipRecordService;
import icu.mhb.mybatisplus.plugln.core.JoinLambdaWrapper;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.LocalTime;

/**
 * 会员购买记录 (VipRecord)表服务实现类
 *
 * @author lyh
 * @since 2023-05-06 13:18:27
 */
@Service("vipRecordService")
public class VipRecordServiceImpl extends MyServiceImpl<VipRecordMapper, VipRecord> implements VipRecordService {

    @Override
    public MyPage<VipRecord> recordPage(VipRecordQueryReq req) {
        LocalDateTime start = null, end = null;
        if (req.getStartTime() != null) {
            start = LocalDateTime.of(req.getStartTime(), LocalTime.of(0, 0, 0));
        }
        if (req.getEndTime() != null) {
            end = LocalDateTime.of(req.getEndTime(), LocalTime.of(23, 59, 59));
        }
        JoinLambdaWrapper<VipRecord> wrapper = new JoinLambdaWrapper<>(VipRecord.class);
        wrapper.orderByDesc(VipRecord::getCreateTime)
                .gt(start != null, VipRecord::getCreateTime, start)
                .lt(end != null, VipRecord::getCreateTime, end)
                .eq(req.getVipId() != null, VipRecord::getVipId, req.getVipId())
                .leftJoin(FrontUser.class, FrontUser::getId, VipRecord::getUserId)
                .like(StrUtil.isNotBlank(req.getUserName()), FrontUser::getAccount, req.getUserName())
                .selectAs(FrontUser::getAccount, "userName")
                .end()
                .leftJoin(VipConfig.class, VipConfig::getId, VipRecord::getVipId)
                .selectAs(VipConfig::getName, "vipName")
                .end();
        return baseMapper.joinSelectPage(req.getPage(), wrapper, VipRecord.class);
    }
}

